package com.example.base.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.base.R;
import com.example.base.activity.LoginActivity;
import com.example.base.bean.QuestionThree;
import com.example.base.bean.QuestionTwo;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
  public List<QuestionThree> getMdataList() {
    return mdataList;
  }

  private List<QuestionThree> mdataList = new ArrayList<>();
  private Context context;
  private String loginusername;

  public RecyclerViewAdapter(Context context, List<QuestionThree> dataList) {
    this.mdataList = dataList;
    this.context = context;
  }

  @Override
  public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view_question, parent, false);
    ViewHolder viewHolder = new ViewHolder(itemView);
    return viewHolder;
  }

  @Override
  public void onBindViewHolder(@NonNull @NotNull RecyclerViewAdapter.ViewHolder holder, int position) {
    QuestionThree questionThree = mdataList.get(position);
    holder.tv_from.setText(questionThree.getAuthor());
    holder.tv_time.setText(questionThree.getNiceDate());
    holder.tv_title.setText(questionThree.getTitle());
    holder.tv_name.setText(questionThree.getChapterName());
    holder.tv_text.setText(Html.fromHtml(questionThree.getDesc()));
    holder.itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (listener != null) {
          listener.onClick(position);
        }
      }
    });

    holder.imageView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (loginusername != null && loginusername.length() != 0) {
          String urileft = "https://www.wanandroid.com/lg/collect/";
          int id;
          String uriRight = "/json";
          id = questionThree.getId();
          SharedPreferences sp = v.getContext().getSharedPreferences("cookies", Context.MODE_PRIVATE);
          String value1 = sp.getString("loginUserName_wanandroid_com", "null");
          String value2 = sp.getString("loginUserName", "null");
          String value3 = sp.getString("token_pass_wanandroid_com", "null");
          String value4 = sp.getString("token_pass", "null");
          OkHttpClient.Builder client = new OkHttpClient.Builder();
          RequestBody body = new FormBody.Builder()
              .add("id", String.valueOf(id))
              .build();
          Request request = new Request.Builder()
              .url(urileft + id + uriRight)
              .post(body)
              .addHeader("cookie", value1)
              .addHeader("cookie", value2)
              .addHeader("cookie", value3)
              .addHeader("cookie", value4)
              .build();
          Call call = client.build().newCall(request);
          call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

            }
          });
          Toast.makeText(context, "收藏成功", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(context, "请先登录", Toast.LENGTH_LONG).show();
          Intent intent = new Intent(context, LoginActivity.class);
          context.startActivity(intent);
        }
      }
    });
  }

  @Override
  public int getItemCount() {
    return mdataList.size();
  }

  public void setData(List<QuestionThree> data) {
    mdataList.clear();
    mdataList.addAll(data);
  }

  class ViewHolder extends RecyclerView.ViewHolder {
    TextView tv_from;
    TextView tv_time;
    TextView tv_title;
    TextView tv_name;
    TextView tv_text;
    ImageView imageView;

    public ViewHolder(@NonNull View itemView) {
      super(itemView);
      this.tv_from = itemView.findViewById(R.id.tv_qus_from);
      this.tv_time = itemView.findViewById(R.id.tv_qus_time);
      this.tv_title = itemView.findViewById(R.id.tv_qus_title);
      this.tv_name = itemView.findViewById(R.id.tv_qus_name);
      this.tv_text = itemView.findViewById(R.id.tv_qus_text);
      this.imageView = itemView.findViewById(R.id.img_ques);
      SharedPreferences sp = itemView.getContext().getSharedPreferences("cookies", Context.MODE_PRIVATE);
      loginusername = sp.getString("loginUserName_wanandroid_com", "");
    }

  }

  public interface OnItemClickListener {
    void onClick(int position);
  }

  private OnItemClickListener listener;

  //第二步， 写一个公共的方法
  public void setOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
  }


}
